package com.hylanda.pb.service;

import javax.servlet.http.Part;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hylanda.pb.model.ReportModel;
import com.hylanda.pb.service.common.ConstantHelper;
import com.hylanda.util.http.OkHttpUtils;

/**
 * @author zhangy
 * @E-mail:blackoon88@gmail.com
 * @qq:846579287
 * @version created at：2018年1月19日 下午1:58:39 note
 */
@Service
public class PowerBiService {

	@Autowired
	private OkHttpUtils okHttpUtils;
	
	public JSONArray getReportNames(ReportModel model) {
		String url = ConstantHelper.getImportStatus
				.replace("{importId}", model.getImportID())
				.replace("{collectionName}", model.getCollectionName())
				.replace("{workspaceID}", model.getWorkspaceID());

		String response = okHttpUtils.sendPowerbiHttp(url, model.getAccessKey());
		if (response.startsWith(ConstantHelper.ERROR_HEAD)) {
			return null;
		}
		JSONArray result = new JSONArray();
		JSONObject json = JSONObject.parseObject(response);
		JSONArray reports = json.getJSONArray("reports");
		for (int i = 0; i < reports.size(); i++) {
			JSONObject name = new JSONObject();
			JSONObject report = reports.getJSONObject(i);
			name.put("name", report.getString("name"));
			result.add(name);
		}
		return result;
	}
	
	public String getReportIdBydisplayName(ReportModel model){
		String reportID = null;
		String url = ConstantHelper.getImportStatus
				.replace("{importId}", model.getImportID())
				.replace("{collectionName}", model.getCollectionName())
				.replace("{workspaceID}", model.getWorkspaceID());
		String response = okHttpUtils
				.sendPowerbiHttp(url, model.getAccessKey());
		if (response.startsWith(ConstantHelper.ERROR_HEAD)) {
			return null;
		}
		JSONObject json = JSONObject.parseObject(response);
		JSONArray reports = json.getJSONArray("reports");
		for (int i = 0; i < reports.size(); i++) {
			JSONObject report = reports.getJSONObject(i);
			if (report.getString("name").equals(model.getDisplayName())) {
				reportID = report.getString("id");
				break;
			}
		}
		return reportID;
	}

	/**
	 * 判断是否存在
	 * @param model
	 * @return
	 */
	public boolean getReportName(ReportModel model) {
		String url = ConstantHelper.getImportStatus
				.replace("{importId}", model.getImportID())
				.replace("{collectionName}", model.getCollectionName())
				.replace("{workspaceID}", model.getWorkspaceID());

		String response = okHttpUtils.sendPowerbiHttp(url, model.getAccessKey());
		if (response.startsWith(ConstantHelper.ERROR_HEAD)) {
			return false;
		}
		JSONObject json = JSONObject.parseObject(response);
		JSONArray reports=null;
		try {
			reports = json.getJSONArray("reports");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		boolean result=true;
		for (int i = 0; i < reports.size(); i++) {
			JSONObject report = reports.getJSONObject(i);
			if(report.getString("name").equals(model.getDisplayName())){
				result=false;
				break;
			}
		}
		return result;
	}

	public JSONArray getReportIdsBydisplayName(ReportModel model) {
		String url = ConstantHelper.getImportStatus
				.replace("{importId}", model.getImportID())
				.replace("{collectionName}", model.getCollectionName())
				.replace("{workspaceID}", model.getWorkspaceID());

		String response = okHttpUtils.sendPowerbiHttp(url, model.getAccessKey());
		if (response.startsWith(ConstantHelper.ERROR_HEAD)) {
			return null;
		}
		JSONObject json = JSONObject.parseObject(response);
		JSONArray reports = json.getJSONArray("reports");
		return reports;
		
	}
	
	public String importPbix(ReportModel model, String fileName, Part part) {
		String url = okHttpUtils.replacePowerBiParam(
				ConstantHelper.reportImport.replace("{datasetDisplayName}", model.getDisplayName()), model.getCollectionName(),
				model.getWorkspaceID());
		String result = "";
		// 存储相关信息到sql server
		try {
			result = okHttpUtils.httpPost(url, fileName, org.apache.commons.io.IOUtils.toByteArray(part.getInputStream()), model.getAccessKey());
		} catch (Exception e) {
			return e.getMessage();
		}
		return result;
	}
	
	public String getImportStatus(ReportModel model) {
		String url = okHttpUtils.replacePowerBiParam(ConstantHelper.getImportStatus.replace("{importId}", model.getImportID()), model.getCollectionName(), model.getWorkspaceID());						 
		String result = okHttpUtils.sendPowerbiHttp(url, model.getAccessKey());
		return result;		
	}
	
	public String getGateWayDs(ReportModel model) {
		String url = okHttpUtils.replacePowerBiParam(ConstantHelper.getBoundGatewayDatasources.replace("{datasetID}", model.getDataSetID()), model.getCollectionName(), model.getWorkspaceID());																																								 
		String result = okHttpUtils.sendPowerbiHttp(url, model.getAccessKey());
		return result;
	}

	public String patchDatasource(ReportModel model) {
		String url = okHttpUtils.replacePowerBiParam(ConstantHelper.patchGatewayDatasource.replace("{gatewayID}", model.getGateWayID()).replace("{datasourceID}", model.getDatasourceID()), model.getCollectionName(), model.getWorkspaceID());		
		JSONObject json = new JSONObject();
		json.put("credentialType", "Basic");
		JSONObject subjson = new JSONObject();
		subjson.put("username", model.getUserName());
		subjson.put("password", model.getPassword());
		json.put("basicCredentials", subjson);
		String result = okHttpUtils.sendPatchHttp(url, json.toJSONString(), model.getAccessKey());
		return result;
	}	

	public String deleteDataSet(ReportModel model) {
		String url = okHttpUtils.replacePowerBiParam(ConstantHelper.deleteDataset.replace("{datasetID}", model.getDataSetID()), model.getCollectionName(), model.getWorkspaceID());  
		String result = okHttpUtils.sendDeleteHttp(url, model.getAccessKey());
		return result;
	}
}
